package org.dashboard.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dashboard.bean.AdUser;
import org.dashboard.constant.Constant;
import org.dashboard.dao.AdUserMapper;
import org.dashboard.service.AdUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = AdUserService.class)
@CacheConfig(cacheManager = Constant.DEFAULT_CACHE_MANAGER, cacheNames = "user")
public class AdUserServiceImpl implements AdUserService {
    private static final Logger logger = LoggerFactory.getLogger(AdUserServiceImpl.class);

    @Autowired
    private AdUserMapper adUserMapper;

    @Override
    @CacheEvict(allEntries=true)
    public int deleteByPrimaryKey(Integer userId) {
        return adUserMapper.deleteByPrimaryKey(userId);
    }

    @Override
    @CacheEvict(allEntries=true)
    public int insert(AdUser record) {
        adUserMapper.insert(record);
        return record.getUserId();
    }

    @Override
    @CacheEvict(allEntries=true)
    public int insertSelective(AdUser record) {
        adUserMapper.insertSelective(record);
        return record.getUserId();
    }

    @Override
    @Cacheable(key = "'userId:' + #userId.toString()")
    public AdUser selectByPrimaryKey(Integer userId) {
        return adUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    @CacheEvict(allEntries=true)
    public int updateByPrimaryKeySelective(AdUser record) {
        return adUserMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @CacheEvict(allEntries=true)
    public int updateByPrimaryKey(AdUser record) {
        return adUserMapper.updateByPrimaryKey(record);
    }

    @Override
    @Transactional
    @CacheEvict(allEntries = true)
    public boolean deleteByPrimaryKeys(ArrayList<Integer> userIds) {
        for (Integer id : userIds) {
            adUserMapper.deleteByPrimaryKey(id);
        }
        return true;
    }

    @Override
    public int checkUserNameExist(String userName, Integer userId) {
        return adUserMapper.checkUserNameExist(userName, userId);
    }

    @Override
    @Cacheable(key = "'userName:' + #name")
    public AdUser selectByName(String name) {
        return adUserMapper.selectByName(name);
    }

    @Override
    public PageInfo selectAdUsersPaged(String criteria, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AdUser> adUsers = adUserMapper.selectAdUsersSearched(criteria);
        return new PageInfo<>(adUsers);
    }

}
